package com.zxc.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.zxc.database.DataBase;
import com.zxc.domain.TableHeader;
import com.zxc.domain.TableInfo;
import com.zxc.util.DBUtil;


public class LoadDataDAO extends BaseDAO{
	
	public List<Object[]> loadData(DBUtil fromDB,TableInfo fromTable,String sql,int start,int end) throws Exception{
		Connection con = null;
		PreparedStatement st = null;
		ResultSet rs = null;
		try {
			con = fromDB.getConnection();
			st = con.prepareStatement(sql);
			st.setInt(1, start);
			st.setInt(2, end);
			rs = st.executeQuery();
			return getDataObjects(rs,fromTable);
			
		} catch (SQLException e) {
			throw e;
		} finally{
			releaseResource(con,st,rs);
		}
	}
	
	private List<Object[]> getDataObjects(ResultSet rs,TableInfo fromTable) throws SQLException{
		List<TableHeader> list = fromTable.getTableHeaders();
		int count = list.size();
		List<Object[]> objList = new ArrayList<Object[]>(DataBase.PER_SIZE);
		Object[] temp = null;
		while(rs.next()){
			temp = new Object[count];
			for(int i = 0 ; i < count; i++){
				temp[i] = rs.getObject(list.get(i).getHeaderName());
			}
			objList.add(temp);
		}
		return objList;
	}
}
